<?
$action = new Action();
class Action
{
	function Action()
	{
		global $gAct,$gOp,$gSmarty;
		$gSmarty->assign("act",$gAct);
		switch($gOp)
		{
			case "firesession":
				break;
			case "saveFile":
			    $this->saveFile();
				break;
			case "saveFileImg":
			    $this->saveFileImg();
				break;
			case "detail":
			    $this->detail();
				break;
			case "ajaxType":
			    $this->ajaxType();
				break;
			case "ajaxDel":
			    $this->ajaxDel();
				break;
			case "saveInfo":
			     $this->saveInfo();
			    break;
			case "ajaxLstDir": 
				$this->ajaxLstDir();
			    break;
			case "ajaxSaveDir":
			    $this->ajaxSaveDir();
			    break;
			case "ajaxDelDir":
			    $this->ajaxDelDir();
			    break;
			case "ajaxDirFile":
			    $this->ajaxDirFile();
			    break;
			case "ajaxRemoveFile":
			    $this->ajaxRemoveFile();
				break;
			case "ajaxSaveFileType":
			    $this->ajaxSaveFileType();
				break;
			case "removeFileDirType":
			    $this->removeFileDirType();
				break;
			case "removeAllFileDirType":
			    $this->removeAllFileDirType();
				break;
			case "doAddApplyList":
			    $this->doAddApplyList();
			    break;
			case "getStFileCount":
			    $this->getStFileCount();
			    break;
            case "getDirGroup":
                $this->getDirGroup();
                break;
            case "mainData":
                $this->mainData();
                break;
            case "unpost":
                $this->unpost();
                break;
			default:
				$this->index();
				break;
		}
	}
	function unpost()
	{
		global $gSmarty,$gAct,$gOp,$INCOME,$DB,$gWeb,$GJson,$gCurTime,$GJson,$_SESSION;
		$id = $INCOME['id'];
		$isSub = $INCOME['isSub'];
		if($id)
		{
			$con = "";
			if($_SESSION['adminUser']['roleId']!=1 && $_SESSION['adminUser']['roleId']!=4)  //非管理员 审批人
			{
				$con = " and uid=".$_SESSION['adminUser']['id'];
			}
			if($isSub=="1") //下架主题
			{
				$table = "res_file_subject";
				$DB->db_query("update res_file set isPost=2 where sid=".$id.$con);
			}
			else if($isSub=="0") //下架素材
			{
				$table = "res_file";
			}
			if($table)
			{
				$DB->db_query("update ".$table." set isPost=2 where id=".$id.$con);
				$data = array("st"=>"ok");
			}

			echo $GJson->encode($data);
		}
		else
		{
			$data = array("st"=>"err");
			echo $GJson->encode($data);
		}
	}
	function mainData()
	{
		global $gSmarty,$gAct,$gOp,$INCOME,$DB,$gWeb,$GJson,$gCurTime,$GJson,$_SESSION;
		exit;
		$DB->db_query("SELECT id,dirIds FROM `res_file` WHERE isDel=0");
		$abc = array();
		while($r = $DB->db_fetch_row())
		{
			$abc[] = array("id"=>$r['id'],"dirIds"=>$r['dirIds']);
		}
		foreach($abc as $v)
		{
			$DB->db_query("update res_app_file set dirIds='".$v['dirIds']."' where id=".$v['id']);
		}
		exit;
		$DB->db_query("SELECT * FROM `res_app_file` WHERE uid=54");
		$abc = array();
		while($r = $DB->db_fetch_row())
		{
			$abc[] = array("id"=>$r['id'],"fid"=>$r['fid']);
		}
		foreach($abc as $v)
		{
			$fff = $DB->db_query_first("select * from res_file where id=".$v['fid']." and uid=54");
			echo "update res_app_file set sid=".$fff['sid']." where id=".$v['id']."<br/>";
			$DB->db_query("update res_app_file set sid=".$fff['sid']." where id=".$v['id']);
		}
		exit;
		$DB->db_query("SELECT * FROM `res_app_file` WHERE isChecked=1 and sid !=0 group by sid");
		$abc = array();
		while($r = $DB->db_fetch_row())
		{
			$abc[] = array("sid"=>$r['sid'],"ps"=>$r['checkTime']);
		}
		foreach($abc as $v)
		{
			$DB->db_query("update res_file_subject set isPost=1,postTime='".$v['ps']."' where id=".$v['sid'],true);
		}
		exit;
		$DB->db_query("SELECT * FROM res_app_file");
		$abc = array();
		while($r = $DB->db_fetch_row())
		{
			$abc[] = array("c"=>$r['isChecked'],"fid"=>$r['fid']);
		}
		$update = array();
		foreach($abc as $v)
		{
			$first = $DB->db_query_first("select * from res_file where id=".$v['fid']);
			if($v['c']==1) //已发布
			{
			    if($first['isPost']!=1)
			    {
			    	echo $v['c']."====".$first['isPost']." select * from res_file where id=".$v['fid']."<br/>";
			    	echo "update res_file set isPost=1 where id=".$v['fid']."<br/>";
			    	$update[] = array("id"=>$v['fid'],"isPost"=>1);
			    }
			}
			else if($v['c']=="0")
			{
				if($first['isPost']!=5)
			    {
			    	echo $v['c']."=".$first['isPost']."select * from res_file where id=".$v['fid']."<br/>";
			    	$update[] = array("id"=>$v['fid'],"isPost"=>5);
			    }
			}
		}
		foreach($update as $v)
		{
			$DB->db_query("update res_file set isPost=".$v['isPost']." where id=".$v['id']);
		}
        exit;
		$DB->db_query("SELECT * FROM res_file_subject where dirIds = ',0,'");
		$ur= array();
		while($r = $DB->db_fetch_row())
		{
			$ur[] = $r['id'];
		}
		if(count($ur)>0)
		{
			foreach($ur as $v)
			{
				$fir = $DB->db_query_first("select * from res_file where sid=".$v);
				if($fir['dirIds'] && $fir['dirNames'])
				{
					$DB->db_query("update res_file_subject set dirIds='".$fir['dirIds']."',dirNames='".$fir['dirNames']."' where id=".$v);
				}
			}
		}
		echo count($ur);
		exit;
		//升级有主题的
		$sql = "SELECT * FROM res_file where isGroupTop>0";
		//echo $sql;
		$DB->db_query($sql);
		$games = array();
		//$games[] = array("id"=>0,"name"=>"全部目录");
        $zhuanTi = array();
		while($r=$DB->db_fetch_row())
		{
			 $zhuanTi[] =$r;
		}
		$i = 0;
		foreach($zhuanTi as $v)
		{
			$dqf = $DB->db_query_first("select count(*) as num from res_file where zhuanTi='".$v['zhuanTi']."' and isGroupTop=0");
			$v['isGroupTop'] = $dqf['num'];
			$zhuanTi[$i] = $v;
			$i++;
		}
		foreach($zhuanTi as $k)
		{
			unset($k['id']);
			if(empty($k['avaliDate']) || $k['avaliDate']=="")
			{
				unset($k['avaliDate']);
			}
			unset($k['year']);
			unset($k['carId']);
			unset($k['typeId']);
			unset($k['styleId']);
			unset($k['yunUploadSTime']);
			unset($k['yunUploadETime']);
			unset($k['edtTime']);
			if(empty($k['edtTime']) || $k['edtTime']=="")
			{
				unset($k['edtTime']);
			}
			if(empty($k['postTime']) || $k['postTime']=="")
			{
				unset($k['postTime']);
			}
			unset($k['sid']);
			$r = $DB->db_query_first("select * from res_file_subject where fid='".$k['fid']."'");
			if(empty($r))
			{
				$sid = $DB->db_form_insert("res_file_subject",$k);
				$DB->db_query("update res_file set sid=$sid where zhuanTi='".$k['zhuanTi']."'");
			}
		}
		//升级没有主题的文件的 变成有主题的文件
		$sql = "SELECT * FROM res_file where isGroupTop = 0 group by zhuanTi";
		//echo $sql;
		$DB->db_query($sql);
		$games = array();
		//$games[] = array("id"=>0,"name"=>"全部目录");
        $zhuanTi = array();
		while($r=$DB->db_fetch_row())
		{
			 $zhuanTi[] =$r;
		}
		foreach($zhuanTi as $k)
		{
			$sub = $DB->db_query_first("select * from res_file_subject where zhuanTi='".$k['zhuanTi']."'");
			if(empty($sub))
			{
			unset($k['id']);
			if(empty($k['avaliDate']) || $k['avaliDate']=="")
			{
				unset($k['avaliDate']);
			}
			unset($k['year']);
			unset($k['carId']);
			unset($k['typeId']);
			unset($k['styleId']);
			unset($k['yunUploadSTime']);
			unset($k['yunUploadETime']);
			unset($k['edtTime']);
			if(empty($k['edtTime']) || $k['edtTime']=="")
			{
				unset($k['edtTime']);
			}
			if(empty($k['postTime']) || $k['postTime']=="")
			{
				unset($k['postTime']);
			}
			unset($k['sid']);
				$resFile = $DB->db_query_first("select count(*) as num from res_file where zhuanTi='".$k['zhuanTi']."'");
				$k['isGroupTop'] = $resFile['num'];
				$sid = $DB->db_form_insert("res_file_subject",$k);
				$DB->db_query("update res_file set sid=".$sid." where zhuanTi='".$k['zhuanTi']."'");
				
			}
		}
		
		//升级申请列表
		$DB->db_query("select id,sid from res_file where id in(select fid from res_app_file where sid=0)");
		$ids = array();
		while($r=$DB->db_fetch_row())
		{
			$ids[] = array("i"=>$r['id'],"s"=>$r['sid']);
		}
		echo count($ids);
		if(count($ids)>0)
		{
			foreach($ids as $v)
			{
				$DB->db_query("update res_app_file set sid=".$v['s']." where fid=".$v['i']);
			}
		}
		echo "finish";
	}
	function getDirGroup()
	{
		global $gSmarty,$gAct,$gOp,$INCOME,$DB,$gWeb,$GJson,$gCurTime,$GJson,$_SESSION;
		$iPerPage = 16;
		if($_SESSION['adminUser']['roleId']!=1 && $_SESSION['adminUser']['roleId']!=4)
		{
			$con[] = "uid=".$_SESSION['adminUser']['id'];
		}
		if($INCOME['f'])
		{
			$con[] = "name like '%".$INCOME['f']."%'";
		}
		if($INCOME['id'])
		{
			$con[] = "dirIds like '%,".$INCOME['id'].",%'";
		}
		$con[] = "isDel=0";
		if(is_array($con))
		{
			$con = " where ".implode(" and ",$con);
		}
		 
		$sql = "SELECT id,zhuanTi,isGroupTop as num,carIds FROM res_file_subject $con order by id";
		//echo $sql;
		$DB->db_query($sql);
		$games = array();
		//$games[] = array("id"=>0,"name"=>"全部目录");
        $zhuanTi = array();
		while($r=$DB->db_fetch_row())
		{
			//echo $r['carIds'];
			if(empty($r['carIds'])||$r['carIds']=="")
			{
				$t = 0;
			}
			else
			{
				$t = 1;
			}
			$games[] = array("name"=>$r['zhuanTi'],"count"=>$r['num'],"id"=>$r['id'],"t"=>$t);
		}
		//echo "SELECT count(*) as num FROM res_file where zhuanTi not in(".implode(",",$zhuanTi).") and dirIds like '%,".$INCOME['id'].",%'";
		$r = $DB->db_query_first("SELECT count(*) as num FROM res_file where sid=0 and dirIds like '%,".$INCOME['id'].",%' and isDel=0");
		$gamesr[] = array("name"=>"未划分主题的文件","count"=>$r['num'],"id"=>0);
		$data = array("data"=>array_merge($gamesr,$games));
		echo $GJson->encode($data);
	}
	function getStFileCount()
	{
		global $gSmarty,$gAct,$gOp,$INCOME,$DB,$gWeb,$GJson,$gCurTime,$GJson,$_SESSION;
		if($_SESSION['adminUser']['roleId']!=1 && $_SESSION['adminUser']['roleId']!=4)
		{
		    $con[] = "uid=".$_SESSION['adminUser']['id'];
		}
		if($INCOME['sid']!=-1)
		{
		 	$con[] = "sid=".$INCOME['sid'];
		}
		
		 
		if(count($con)>0)
		{
			$con = implode(" and ",$con);
			$con = " and ".$con;
		}
		$sql = "SELECT isPost,count(*) as num FROM res_file where isDel=0 and isGroupTop=0 and dirIds like '%,".$INCOME['did'].",%' ".$con." group by isPost";
		//echo $sql;
		$DB->db_query($sql);
		$st["0"] = array("n"=>"未完善信息","v"=>0,"c"=>0);
		$st["3"] = array("n"=>"已完善信息","v"=>3,"c"=>0);
		$st["4"] = array("n"=>"提交审批","v"=>4,"c"=>0);
		$st["5"] = array("n"=>"审批中","v"=>5,"c"=>0);
		$st["1"] = array("n"=>"已发布","v"=>1,"c"=>0);
		$st["2"] = array("n"=>"已下架","v"=>2,"c"=>0);
		while($r = $DB->db_fetch_row())
		{
			if($st[$r['isPost']])
			{
 
				$st[$r['isPost']] = array("n"=>$st[$r['isPost']]['n'],"v"=>$r['isPost'],"c"=>$r['num']);
			}
		}
		foreach($st as $k=>$v)
		{
			$data[] = $v;
		}
		echo $GJson->encode($data);
	}
	function doAddApplyList()
	{
		global $gSmarty,$gAct,$gOp,$INCOME,$DB,$gWeb,$GJson,$gCurTime,$GJson;
		if($INCOME['id'])
		{
			
		}
		else if($INCOME['dirId'])
	    {
			if($INCOME['sid'])
			{
				$sidCon = " and sid=".$INCOME['sid'];
			}
			$DB->db_query("select id from res_file where dirIds like '%,".$INCOME['dirId'].",%' and isPost=3 and isDel=0 ".$sidCon);
			if($DB->db_fetch_num()>0)
			{
				$ids = array();
				while($r=$DB->db_fetch_row())
				{
					$ids[] = $r['id'];
				}
				$INCOME['id'] = implode(",",$ids);
			}
	    }
 
	    if(strlen($INCOME['id'])>0)
	    {
	    	$DB->db_query("update res_file set isPost=4 where id in(".$INCOME['id'].") and isPost=3");
	    	//if($INCOME['sid'])
	    	//{
	    	//	$DB->db_query("update res_file_subject set isPost=4 where id=".$INCOME['sid']." and isPost=3");
	    	//}
	    }
	    $data = array("st"=>"ok","msg"=>"成功","id"=>$ids[0]);
	    
 		echo $GJson->encode($data);
	}
	function removeAllFileDirType()
	{
		global $gSmarty,$gAct,$gOp,$INCOME,$DB,$gWeb,$GJson,$gCurTime;
		if($INCOME['id']=="0" && $INCOME['pdid']>0)
		{
			$DB->db_query("select id from res_file where dirIds like '%,".$INCOME['pdid'].",%' and isPost=0 and isDel=0");
			if($DB->db_fetch_num()>0)
			{
				$ids = array();
				while($r=$DB->db_fetch_row())
				{
					$ids[] = $r['id'];
				}
				$INCOME['id'] = implode(",",$ids);
			}
		}
		if($INCOME['id'])
		{
			$DB->db_query("update res_file set typeIds='',typeNames='',carIds='',carNames='' where id in(".$INCOME['id'].")");
		}
		$ids = explode(",",$INCOME['id']);
		$data = array("st"=>"ok","msg"=>"成功","id"=>$ids[0]);
 		echo $GJson->encode($data);
	}
	function removeFileDirType()
	{
		global $gSmarty,$gAct,$gOp,$INCOME,$DB,$gWeb,$GJson,$gCurTime;
		$r = $DB->db_query_first("select * from res_file where id=".$INCOME['id']." and isDel=0");
		if($INCOME['type']=="1")
		{
			$t = explode(",",$r['typeIds']);
			$tn = explode(",",$r['typeNames']);
			unset($t[$INCOME['tid']]);
			unset($tn[$INCOME['tid']]);
			$DB->db_query("update res_file set typeIds=',".implode(",",$t).",',typeNames=',".implode(",",$tn).",' where id=".$INCOME['id']);
		}
		if($INCOME['type']=="2")
		{
			$c = explode(",",$r['carIds']);
			$cn = explode(",",$r['carNames']);
			unset($c[$INCOME['tid']]);
			unset($cn[$INCOME['tid']]);
			$DB->db_query("update res_file set carIds=',".implode(",",$c).",',carNames=',".implode(",",$cn).",' where id=".$INCOME['id']);
		}
		$data = array("st"=>"ok","msg"=>"成功");
 		echo $GJson->encode($data);
	}
	function ajaxSaveFileType()
	{
		global $gSmarty,$gAct,$gOp,$INCOME,$DB,$gWeb,$GJson,$gCurTime;
		if($INCOME['id']=="0" && $INCOME['pdid']>0)
		{
			$DB->db_query("select id from res_file where dirIds like '%,".$INCOME['pdid'].",%' and isPost=0 and isDel=0");
			if($DB->db_fetch_num()>0)
			{
				$ids = array();
				while($r=$DB->db_fetch_row())
				{
					$ids[] = $r['id'];
				}
				$INCOME['id'] = implode(",",$ids);
			}
		}
		if(strpos($INCOME['id'],",")===false)
		{
			$r = $DB->db_query_first("select * from res_file where id=".$INCOME['id']." and isDel=0");
			$c = explode(",",$r['carIds']);
			$t = explode(",",$r['typeIds']);
 			$cn = explode(",",$r['carNames']);
			$tn = explode(",",$r['typeNames']);
			if($r['brandId'] && $r['carId'])
			{
				if(!in_array($r['brandId'] .":". $r['carId'],$c))
				{
					$c[] = $r['brandId'] .":". $r['carId'];
					$cn[] = $r['brandName'] .":". $r['carName'];
				}
			}
			if($r['typeId'] && $r['styleId'])
			{
				if(!in_array($r['typeId'] .":". $r['styleId'],$t))
				{
					$t[] = $r['typeId'] .":". $r['styleId'];
					$tn[] = $r['typeName'] .":". $r['styleName'];
				}
			}
		}
		else
		{
			$fids = explode(",",$INCOME['id']);
			$r = $DB->db_query_first("select * from res_file where id=".$fids[0]." and isDel=0");
			$c = explode(",",$r['carIds']);
			$t = explode(",",$r['typeIds']);
 			$cn = explode(",",$r['carNames']);
			$tn = explode(",",$r['typeNames']);
			if($r['brandId'] && $r['carId'])
			{
				if(!in_array($r['brandId'] .":". $r['carId'],$c))
				{
					$c[] = $r['brandId'] .":". $r['carId'];
					$cn[] = $r['brandName'] .":". $r['carName'];
				}
			}
			if($r['typeId'] && $r['styleId'])
			{
				if(!in_array($r['typeId'] .":". $r['styleId'],$t))
				{
					$t[] = $r['typeId'] .":". $r['styleId'];
					$tn[] = $r['typeName'] .":". $r['styleName'];
				}
			}
		}
		if($INCOME['dbbrandId'] || $INCOME['dbcarId'])
		{
			if(!in_array($INCOME['dbbrandId'] .":". $INCOME['dbcarId'],$c))
			{
				$c[] = $INCOME['dbbrandId'] .":". $INCOME['dbcarId'];
				$cn[] = $INCOME['dbbrandName'] .":". $INCOME['dbcarName'];
			}
		}
		if($INCOME['dbtypeId']|| $INCOME['dbstyleId'])
		{
			if(!in_array($INCOME['dbtypeId'] .":". $INCOME['dbstyleId'],$t))
			{
				$t[] = $INCOME['dbtypeId'] .":". $INCOME['dbstyleId'];
				$tn[] = $INCOME['dbtypeName'] .":". $INCOME['dbstyleName'];
			}
		}
        $c = array_filter($c);
 		$cn = array_filter($cn);
 		$t = array_filter($t);
 		$tn = array_filter($tn);
		$sql = "update res_file set carIds=',".implode(",",$c).",',carNames=',".implode(",",$cn).",',typeIds=',".implode(",",$t).",',typeNames=',".implode(",",$tn).",' where id in(".$INCOME['id'].")";
		$DB->db_query($sql);
		//echo $sql;
		echo "<script>parent.saveTypeResult(1);</script>";
	}
	function ajaxRemoveFile()
	{
		global $gSmarty,$gAct,$gOp,$INCOME,$DB,$gWeb,$GJson,$gCurTime;
		$did = $INCOME['did'];
		$id = $INCOME['id'];
		if($did && $id)
		{
			$did = explode(",",$did);
			$id = explode(",",$id);
			
			foreach($id as $i)
			{
					$r = $DB->db_query_first("select * from res_file where id=".$i);
					$dbDid = array();
					if($r['dirIds'])
					{
						$dbDid = explode(",",$r['dirIds']);
					}
					$newdbdid = array();
					foreach($dbDid as $v)
					{
						if($v)
						{
							$newdbdid[] = $v;
						}
					}
					$dbDid = $newdbdid;
					$newUpdate = array();
					foreach($dbDid as $didr)
					{
						$isHave = false;
						foreach($did as $d)
						{
                           if($didr==$d)
						   {
							   $isHave = true;
						   }
						}
						if(!$isHave)
						{
							$newUpdate[]= $didr;
						}
					}
					$DB->db_query("update res_file set  dirIds=',".implode(",",$newUpdate).",' where id=".$i);
			}
		}
		$data = array("st"=>"ok","msg"=>"成功");
 		echo $GJson->encode($data);
	}
	function ajaxDirFile()
	{
		global $gSmarty,$gAct,$gOp,$INCOME,$DB,$gWeb,$GJson,$gCurTime;
		$did = $INCOME['did'];
		$id = $INCOME['id'];
		if($did && $id)
		{
            $arrDid = explode(",",$did);
			$did = $did.",";
			$id = explode(",",$id);
				foreach($id as $i)
				{
					$r = $DB->db_query_first("select * from res_file where id=".$i);
					$dbDid = array();
					if($r['dirIds'])
					{
						$dbDid = explode(",",$r['dirIds']);
					}
					$newdbdid = array();
					foreach($dbDid as $v)
					{
						if($v)
						{
							$newdbdid[] = $v;
						}
					}
					$dbDid = $newdbdid;
					foreach($arrDid as $d)
					{
						if(!in_array($d,$dbDid))
						{
							$dbDid[] = $d;
						}
					}
					$DB->db_query("update res_file set  dirIds=',".implode(",",$dbDid).",' where id=".$i);
				}
			 
		}
		$data = array("st"=>"ok","msg"=>"成功");
 		echo $GJson->encode($data);
	}
	function ajaxDel()
	{
		global $gSmarty,$gAct,$gOp,$INCOME,$DB,$gWeb,$GJson,$gCurTime,$_SESSION;
		if($_SESSION['adminUser']['roleId']== "2")
		{
			$con = " and uid=".$_SESSION['adminUser']['id'];
		}
		$id = $INCOME['id'];
		if($id)
		{
			$DB->db_query("update res_file set isDel=1 where id in(".$id.") ".$con);
		}
		$data = array("st"=>"ok","msg"=>"删除成功");
 		echo $GJson->encode($data);
	}
	function ajaxDelDir()
	{
		global $gSmarty,$gAct,$gOp,$INCOME,$DB,$gWeb,$GJson,$gCurTime,$_SESSION;
		if($INCOME['id'])
		{
			//$r = $DB->db_query_first("select * from res_file where dirIds like ',%".$INCOME['id'].",%' and isDel=0 ");
			//if($r)
			//{
			//	$data = array("st"=>"err","msg"=>"删除失败 该目录下有文件");
			//}
			//else
			//{
			//	$DB->db_query("delete from res_dir where id=".$INCOME['id']." and uid=".$_SESSION['adminUser']['id']);
			//	$data = array("st"=>"ok","msg"=>"删除成功");
			//}
			if($_SESSION['adminUser']['roleId']!=1 && $_SESSION['adminUser']['roleId']!=4)
			{
				$con = " and uid=".$_SESSION['adminUser']['id'];
			}
			if($INCOME['sid']!=-1)
			{
				//$DB->db_query("delete from res_file where dirIds like ',%".$INCOME['id'].",%' and sid=".$INCOME['sid']." ".$con);
				//$DB->db_query("delete from res_file_subject where id=".$INCOME['sid']." ".$con);
				$DB->db_query("update res_file set isDel=1 where dirIds like ',%".$INCOME['id'].",%' and sid=".$INCOME['sid']." ".$con);
				$DB->db_query("update res_file_subject set isDel=1 where id=".$INCOME['sid']." ".$con);
				$DB->db_query("delete from res_app_file where sid=".$INCOME['sid']." ".$con);
			}
			else
			{
//				$DB->db_query("delete from res_file where dirIds like ',%".$INCOME['id'].",%' ".$con);
//				$DB->db_query("delete from res_dir where id=".$INCOME['id']." ".$con);
				$DB->db_query("update res_file set isDel=1 where dirIds like ',%".$INCOME['id'].",%' ".$con);
				$DB->db_query("update res_dir set isDel=1 where id=".$INCOME['id']." ".$con);
				$DB->db_query("update res_file_subject set isDel=1 where dirIds like ',%".$INCOME['id'].",%' ".$con);
				$DB->db_query("delete from res_app_file where dirIds like ',%".$INCOME['id'].",%' ".$con);
			}

			$data = array("st"=>"ok","msg"=>"删除成功");
			echo $GJson->encode($data);
		}
	}
	function ajaxSaveDir()
	{
		global $gSmarty,$gAct,$gOp,$INCOME,$DB,$gWeb,$GJson,$gCurTime;

		if($INCOME['id'])
		{
			$r = $DB->db_query_first("select * from res_dir where name='".$INCOME['name']."' and id!=".$INCOME['id']);
			if($r)
			{
				echo "exist";
				exit;
			}
			$f['edtTime'] =$gCurTime;
			$f['name'] = $INCOME['name'];
			$DB->db_form_update("res_dir",$f,"id=".$INCOME['id']);
		}
		else
		{
			$r = $DB->db_query_first("select * from res_dir where name='".$INCOME['name']."'");
			if($r)
			{
				echo "exist";
				exit;
			}
			$f['name'] = $INCOME['name'];
			$f['uid']= $_SESSION['adminUser']['id'];
			$f['uname']= $_SESSION['adminUser']['name'];
		 	$f['addTime'] =$gCurTime;
			$DB->db_form_insert("res_dir",$f);
		}
		echo "ok";
	}
	function ajaxLstDir()
	{
		global $gSmarty,$gAct,$gOp,$INCOME,$DB,$gWeb,$GJson,$gPageName;
		$iPerPage = 16;
		if($_SESSION['adminUser']['roleId']!=1 && $_SESSION['adminUser']['roleId']!=4)
		{
			$con[] = "uid=".$_SESSION['adminUser']['id'];
		}
		if($INCOME['f'])
		{
			$con[] = "name like '%".$INCOME['f']."%'";
		}
		$con[] = "isDel=0";
		if(is_array($con))
		{
			$con = " where ".implode(" and ",$con);
		}
		$r = $DB->db_query_first("SELECT count(*) as num FROM res_dir $con order by id desc");
		$allNum = $r['num'];
		$totalPage = ceil($allNum/$iPerPage);
		$ajax = $INCOME['ajax']?$INCOME['ajax']:"getPage";
		$queryUrl = $gPageName."?act=".$gAct."&op=".$gOp."&f=".$INCOME['f']."&ajax=".$ajax;
		$pageClass = new MyPage(array (
		'total' => $allNum,
		'perpage' => $iPerPage,
		'url' => $queryUrl
		));
		$pageClass->open_Ajax($ajax);
		$page = empty ($INCOME['page']) ? 1 : $INCOME['page'];
		$iStartNo = ($page -1) * $iPerPage;
		$sql = "SELECT * FROM res_dir $con order by id desc LIMIT {$iStartNo}, {$iPerPage}";
		$DB->db_query($sql);
		$games = array();
		//$games[] = array("id"=>0,"name"=>"全部目录");
		while($r=$DB->db_fetch_row())
		{
			$games[] = array("id"=>$r['id'],"name"=>$r['name']);
		}
		$pageNav = $pageClass->simpleShow2();
		$data = array("data"=>$games,"pageNav"=>$pageNav,"page"=>$page,"total"=>$totalPage);
		echo $GJson->encode($data);
	}
	function saveInfo()
	{
		global $gSmarty,$gAct,$gOp,$INCOME,$DB,$gWeb,$GJson,$gCurTime;
		if($_SESSION['adminUser']['roleId']!=1 && $_SESSION['adminUser']['roleId']!=4)
		{
		    $con = " and uid=".$_SESSION['adminUser']['id'];
		}
		if($INCOME['id'] || $INCOME['pdid'] || $INCOME['dbsid'])
		{
			if($INCOME['isPost']==-1) //下架 专题
			{
				if($INCOME['isUpdateAll']=="1" && $INCOME['dbsid']) //下架主题下的所有文件
				{
					$DB->db_query("update res_file set isPost=2 where sid=".$INCOME['dbsid']." $con ");
					$DB->db_query("update res_file_subject set isPost=2 where id=".$INCOME['dbsid']." $con ");
				}
				else
				{
					$DB->db_query("update res_file set isPost=2 where id in(".$INCOME['id'].") $con ");
					$xx = $DB->db_query_first("select count(*) as v from res_file where sid=".$INCOME['dbsid']." and isPost!=2");
					if(empty($xx['v']) || $xx['v']=="0")
					{
						$DB->db_query("update res_file_subject set isPost=2 where id=".$INCOME['dbsid']." $con ");
					}
				}
			}
			else
			{
				$this->updateFileInfo();
			}
			if($INCOME['src']=="ajax")
			{
				$data = array("st"=>"ok","msg"=>"成功");
 				echo $GJson->encode($data);
			}
			else
			{
		 		echo '<script>parent.saveResult(1,"'.$INCOME['optype'].'");</script>';
	     	}
		}
	}
	function updateFileInfo()
	{
			global $gSmarty,$gAct,$gOp,$INCOME,$DB,$gWeb,$GJson,$gCurTime;
			//更新数据
			$f = $DB->getDBFields($INCOME);
			$f['zhuanTi'] = str_replace(PHP_EOL,'', $f['zhuanTi']);
			$f['zhuanTi'] = trim($f['zhuanTi']);
			$f['edtTime'] = $gCurTime;
			if($INCOME['isUpdateAll']=="4") //只更新主题信息
			{
				unset($f['sid']);
				// 统计 主题下的文件数量
            	$resCount = $DB->db_query_first("select count(*) as num from res_file where sid=".$INCOME['dbsid']." and isDel=0");
            	$f['isGroupTop'] = $resCount['num'];
				$DB->db_form_update("res_file_subject",$f,"id = ".$INCOME['dbsid'],false);
				$file['carIds'] = $f['carIds'];
				$file['typeIds'] = $f['typeIds'];
				$file['carNames'] = $f['carNames'];
				$file['typeNames'] = $f['typeNames'];
				$DB->db_form_update("res_file",$file,"sid = ".$INCOME['dbsid'],false);
				return;
			}
			$updateRow = array();
			if($INCOME['isUpdateAll']=="1") //如果是目录所有文件
			{
				if($INCOME['dbsid']) //分主题修改
				{
					$sidCon = " and sid=".$INCOME['dbsid'];
				}
				$DB->db_query("select id,isPost from res_file where dirIds like '%,".$INCOME['pdid'].",%' and isGroupTop=0 and isDel=0".$sidCon); //目录下的所有文件的id
				//echo $DB->db_fetch_num();
				if($DB->db_fetch_num()>0)
				{
					while($r=$DB->db_fetch_row())
					{
						$updateRow[]= array("id"=>$r['id'],"isPost"=>$r['isPost']);
					}
				}
			}
			else
			{
				$DB->db_query("select id,isPost from res_file where id in(".$INCOME['id'].") $con");
            	while($r=$DB->db_fetch_row())
            	{
            		$updateRow[]= array("id"=>$r['id'],"isPost"=>$r['isPost']);
            	}
			}

			if($INCOME['optype']=="post")//发布
			{
				$f['isPost'] = 1;
			}
			if($INCOME['isPost']==-1) //下架
			{
				$f['isPost'] = 2;
			}
            if($INCOME['isUpdateAll']=="1") //先更新主题信息
            {
            	$dbSubject = $DB->db_query_first("select * from res_file_subject where id=".$INCOME['dbsid']); //主题的信息
            	if($dbSubject['isPost']==0) //未完善 就改成 已完善状态
        		{
        			$dbSubject['isPost'] = 3;
        		}
        		unset($f['sid']);
        		$f['isGroupTop'] = count($updateRow);
        		$DB->db_form_update("res_file_subject",$f,"id = ".$dbSubject['id'],false);
        		//$dbSubject = $DB->db_query_first("select * from res_file_subject where id=".$INCOME['dbsid']); //获取的信息
        		$f['isGroupTop'] = 0;
        		$f['sid'] = $INCOME['dbsid'];
        		//$f['carIds'] = $dbSubject['carIds']; //获取主题的数据
        		//$f['typeIds'] = $dbSubject['typeIds'];
        		//$f['carNames'] = $dbSubject['carNames']; 
        		//$f['typeNames'] = $dbSubject['typeNames']; 
            }
            //var_dump($updateRow);
            foreach($updateRow as $v) //更新文件
        	{
        		if($v['isPost']==0) //未完善 就改成 已完善状态
        		{
        			$f['isPost'] = 3;
        		}
        		$DB->db_form_update("res_file",$f,"id = ".$v['id'],false);
        	}
            if($INCOME['dbsid']=="0" && count($updateRow)==1 && $f['zhuanTi']) //修改1个文件，且该文件在未分主题组中，则进行分组
            {
            		$isr = $DB->db_query_first("select id from res_file_subject where zhuanTi='".$f['zhuanTi']."' and dirIds=',".$INCOME['pdid'].",'"); //如果该主题存在
            		if($isr)
            		{
            			$f['sid'] = $isr['id'];
            		}
            		else
            		{
						$k = $DB->db_query_first("select * from res_file where id=".$updateRow[0]['id']);
						$k['mark'] = $k['zhuanTi'];
						unset($k['id']);
						if(empty($k['avaliDate']) || $k['avaliDate']=="")
						{
							unset($k['avaliDate']);
						}
						unset($k['year']);
						unset($k['carId']);
						unset($k['typeId']);
						unset($k['styleId']);
						unset($k['yunUploadSTime']);
						unset($k['yunUploadETime']);
						unset($k['edtTime']);
						if(empty($k['edtTime']) || $k['edtTime']=="")
						{
							unset($k['edtTime']);
						}
						if(empty($k['postTime']) || $k['postTime']=="")
						{
							unset($k['postTime']);
						}
						unset($k['sid']);
						$f['sid'] = $DB->db_form_insert("res_file_subject",$k);
						$DB->db_query("update res_app_file set sid=".$f['sid']." where fid=".$updateRow[0]['id']);
            		}
            		$DB->db_query("update res_app_file set sid=".$f['sid']." where fid=".$updateRow[0]['id']);
            		$DB->db_query("update res_file set sid=".$f['sid']." where id=".$updateRow[0]['id']);

            }
            // 统计 主题下的文件数量
            $resCount = $DB->db_query_first("select count(*) as num from res_file where sid=".$f['sid']." and isDel=0");
            $DB->db_query("update res_file_subject set isGroupTop=".$resCount['num']." where id=".$f['sid']);
	}
	function ajaxType()
	{
		global $gSmarty,$gAct,$gOp,$INCOME,$DB,$gWeb,$GJson;
		$sql = "SELECT * FROM type where parId=".$INCOME['id']." and isHide=0 order by rank desc";
		$DB->db_query($sql);
		$games = array();
		while($r=$DB->db_fetch_row())
		{
			$games[] = array("id"=>$r['id'],"name"=>$r['name']);
		}
		echo $GJson->encode($games);
	}
	function detail()
	{
		global $gSmarty,$gAct,$gOp,$INCOME,$DB,$gWeb,$_SESSION,$gCurTime;
		$rf = $DB->db_query_first("select * from res_file where id=".$INCOME['id']." and isDel=0");
		$rf['path'] = str_replace("s.","sb.",$rf['path']);
		$gSmarty->assign("f",$rf);
		$gSmarty->display("upload/".__FUNCTION__.".tpl");
	}
    function saveFile()
	{
		global $gSmarty,$gAct,$gOp,$INCOME,$DB,$gWeb,$_SESSION,$gCurTime,$gFileType,$GJson;
		 
		//$INCOME['path'] = str_replace("\\","/",urldecode($INCOME['path']));
		//echo urldecode($INCOME['path']);
		$fileInfo = pathinfo($INCOME['path']);
		//var_dump($fileInfo);
		$f['fid'] =  uniqid("file_");
		$newPath = $fileInfo['dirname']."/".$f['fid'].".".strtolower($fileInfo['extension']);
		rename($INCOME['path'],$newPath);
		$f['name'] = $INCOME['name'];
		$f['ext'] =  strtolower($fileInfo['extension']);
		if($f['ext']=="jpeg")
		{
			$f['ext'] = str_replace("jpeg","jpg",$f['ext']);
		}
		if(empty($f['ext']) || $f['ext']=="")
		{
			$data = array("st"=>"ok","msg"=>"处理失败，取不到文件扩展名，解决方法 文件名不要含特殊字符，最好纯字母数字组合","fid"=>$INCOME['fid']);
 			echo $GJson->encode($data);
 			unlink($INCOME['path']);
 			exit;
		}
		$f['path']= $newPath;
		$extType = $gFileType[$f['ext']];
		if($extType['id']==39)
		{
			include "lib/ResizeImage.php";
			//echo $newPath;
			$minPath = str_replace(".","s.",$f['path']);
		    //$gWeb->resizeImage($newPath,$minPath,200,135);
			new ResizeImage($newPath, 200,135, '0',$minPath);
			//$gWeb->resizeImage($f['path'],$minPath,692,550);
			$f['path']= $minPath;
			$minPath = str_replace(".","b.",$f['path']);
			//$gWeb->resizeImage($newPath,$minPath,692,550);
			new ResizeImage($newPath, 692,550, '0',$minPath);
			$minPath = str_replace(".","m.",$f['path']);
			//$gWeb->resizeImage($newPath,$minPath,300,300);
			new ResizeImage($newPath, 300,300, '0',$minPath);
		}
		if($extType)
		{
			$f['fileTypeId'] = $extType['id'];
			$f['fileTypeName'] = $extType['n'];
		}
		else
		{
			$f['fileTypeId'] = "0";
			$f['fileTypeName'] = "";
		}
		$f['dirIds']= ",".$INCOME['dirId'].",";
		$f['dirNames']= ",".$INCOME['dirName'].",";
		$f['uid']= $_SESSION['adminUser']['id'];
		$f['uname']= $_SESSION['adminUser']['name'];
		$f['size'] = filesize($newPath);
		$f['sid'] = $INCOME['sid'];
		if(empty($f['size']))
		{
			$f['size'] = 0;
		}
		$f['addTime'] =$gCurTime;
		$DB->db_form_insert("res_file",$f);
		$data = array("st"=>"ok","msg"=>"成功","fid"=>$INCOME['fid']);
 		echo $GJson->encode($data);
	}
	function index()
	{
		global $gSmarty,$gAct,$gOp,$INCOME,$DB,$gWeb,$_SESSION;
        $gSmarty->assign("roleId",$_SESSION['adminUser']['roleId']);
		$gSmarty->display(basename(__FILE__,'.php')."/".__FUNCTION__.".tpl");
	}
	function saveFileImg()
	{
		global $gSmarty,$gAct,$gOp,$INCOME,$DB,$gWeb,$_SESSION,$gCurTime,$gFileType,$GJson;
		 
		//$INCOME['path'] = str_replace("\\","/",urldecode($INCOME['path']));
		//echo urldecode($INCOME['path']);
		$editId = $INCOME['edit'];
		if($editId)
		{
			$dbfileInfo = $DB->db_query_first("select * from res_file where id=".$editId);
		}
		else
		{
			$data = array("st"=>"err","msg"=>"缺少id");
 			echo $GJson->encode($data);
			exit;
		}
		$fileInfo = pathinfo($INCOME['path']);
		//var_dump($fileInfo);
		$f['fid'] =  $dbfileInfo['fid'];
		$newPath = $fileInfo['dirname']."/".$f['fid'].".".strtolower($fileInfo['extension']);
		rename($INCOME['path'],$newPath);
		$f['name'] = $INCOME['name'];
		$f['ext'] =  strtolower($fileInfo['extension']);
		if($f['ext']=="jpeg")
		{
			$f['ext'] = str_replace("jpeg","jpg",$f['ext']);
		}
		$f['path']= $newPath;
		$DB->db_query("update res_file set pic='".$newPath."' where id=".$editId);
		$extType = $gFileType[$f['ext']];
		if($extType['id']==39)
		{
			include "lib/ResizeImage.php";
			//echo $newPath;
			$minPath = str_replace(".","s.",$f['path']);
		    //$gWeb->resizeImage($newPath,$minPath,200,135);
			new ResizeImage($newPath, 200,135, '0',$minPath);
			//$gWeb->resizeImage($f['path'],$minPath,692,550);
			$f['path']= $minPath;
			$minPath = str_replace(".","b.",$f['path']);
			$maxPic = $minPath;
			//$gWeb->resizeImage($newPath,$minPath,692,550);
			new ResizeImage($newPath, 692,550, '0',$minPath);
			$minPath = str_replace(".","m.",$f['path']);
			//$gWeb->resizeImage($newPath,$minPath,300,300);
			new ResizeImage($newPath, 300,300, '0',$minPath);
		}
		$data = array("st"=>"ok","msg"=>"成功","path"=>$maxPic);
 		echo $GJson->encode($data);
	}
}
?>